var salt = sessionStorage.getItem('usersalt');
///perilInfo/getInfo
var arr3 = new Array();
arr3 = [0, 1, 2];
arr1 = [];

layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		formSelects = layui.formSelects,
		laytpl = layui.laytpl,
		table = layui.table;

	/*验证登录*/
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);

	}

	/*时间选择器*/
	laydate.render({
		elem: '#statdate',
		trigger: 'click'

	});

	laydate.render({
		elem: '#enddate',
		trigger: 'click'

	});

	/*筛选*/
	$('#selerisk').on('click', function() {

		yinhuanlist();

	})

	/*搜索*/
	$('#findscri').on('click', function() {
		var inptext = $('#inptext').val();
		var box = /\s/g;
		var res = inptext.replace(box, '');
		yinhuanlist(res);

	});
	var company;
	/*取出所有的单位*/
	finddanwei();

	function finddanwei() {
		$.ajax({
			url: url + 'baseEnt/getAllEnt',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {

			},
			success: function(data) {
				$('#danwe').empty();
				var opt = '<option value="0" selected="selected" >请选择</option>';
				if(data.code == 0) {
					var list = data.data;
					company = list;
					//					for(var i = 0; i < list.length; i++) {
					//						opt += '<option value="' + list[i].id + '">' + list[i].name + '</option>';
					//					}
					//					$('#danwe').append(opt);
					//					form.render();

				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}

	var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onCheck: zTreeOnCheck
		}
	};
	var zNodes = company;

	$.fn.zTree.init($("#danwe"), setting, zNodes);

	function getval() {
		arr = []
		var treeObj = $.fn.zTree.getZTreeObj("danwe");
		var nodes = treeObj.getCheckedNodes(true),
			v = "";
		for(var i = 0; i < nodes.length; i++) {
			v += nodes[i].name + ",";
			arr.push(nodes[i].id); //获取选中节点的值
		}
		return arr;
	}

	$.fn.zTree.init($("#danwe"), setting, zNodes);
	$('#resclas').click(function() {

		$('#danwe').show();
//		$('#resclas').val('')
	});
	$('#quedingqy').click(function() {
		$('#danwe').hide();
	});

	var n = ''

	function zTreeOnCheck(event, treeId, treeNode) {
		//		nams = "";
		//		var checked = treeNode.checked;
		//				nams+= (treeNode?treeNode.name:"root") + ",";
		var treeObj = $.fn.zTree.getZTreeObj("danwe");
		var nodes = treeObj.getCheckedNodes(true),
			n = "";
		for(var i = 0; i < nodes.length; i++) {
			n += nodes[i].name + ",";
			$('#resclas').val(n)

		}

	};

	var btnyes = 0;
	var btnno = 0;
	var btnclear = 0;
	/*隐患类型按钮*/
	$('#btnyes').on('click', function() {
		btnyes++;
		if(btnyes % 2 == 0) {
			$('#triangle').css('display', 'block');
			$('#btnyes').css('background', 'white');
			arr3.push(0);
		} else {

			$('#triangle').css('display', 'none');
			$('#btnyes').css('background', 'gray');

			for(var i = 0; i < arr3.length; i++) {
				if(arr3[i] == 0) {
					arr3.splice(i, 1);
				}
			}
		}

	});
	$('#btnno').on('click', function() {
		btnno++;
		if(btnno % 2 == 0) {
			$('#triangle1').css('display', 'block');
			$('#btnno').css('background', 'white');
			arr3.push(1);
		} else {
			$('#triangle1').css('display', 'none');
			$('#btnno').css('background', 'gray');
			for(var i = 0; i < arr3.length; i++) {
				if(arr3[i] == 1) {
					arr3.splice(i, 1);
				}
			}
		}

	});
	$('#btnclear').on('click', function() {
		btnclear++;
		if(btnclear % 2 == 0) {
			$('#triangle2').css('display', 'block');
			$('#btnclear').css('background', 'white');
			arr3.push(2);
		} else {
			$('#triangle2').css('display', 'none');
			$('#btnclear').css('background', 'gray');
			for(var i = 0; i < arr3.length; i++) {
				if(arr3[i] == 2) {
					arr3.splice(i, 1);
				}
			}
		}

	});

	var tableIns;
	yinhuanlist();
	//新闻列表
	function yinhuanlist(soutext) {
		var ls = getval();

		if(!soutext) {
			soutext = null;
		}
		tableIns = table.render({
			elem: '#newsList',
			method: 'post',
			url: url + 'perilInfo/getPage',
			where: {
				describe: soutext, //描述
				node: arr3.toString(), //流转状态
				entId: ls.toString(), //单位id
				Token: salt, //用户码
				startTime: $('#statdate').val(), //开始时间
				endTime: $('#enddate').val(), //结束时间

			},
			cellMinWidth: 95,

			page: true,

			height: "full-115",

			id: "newsListTable",
			cols: [
				[
					//      {title: '序号',templet: '#indexTpl'},
					{
						field: 'node',
						align: 'center',
						width: 20,
						templet: function(p) {
							//当前时间
							var date = new Date();
							var seperator1 = "-";
							var year = date.getFullYear();
							var month = date.getMonth() + 1;
							var strDate = date.getDate();
							if(month >= 1 && month <= 9) {
								month = "0" + month;
							}
							if(strDate >= 0 && strDate <= 9) {
								strDate = "0" + strDate;
							}
							var currentdate = year + seperator1 + month + seperator1 + strDate;
							currentdate = Date.parse(currentdate);

							//逾期时间
							var date2 = new Date(p.endTime)

							if(date2 - date + 86400000 >= 0 || p.node != 0) {
								return "<div ></div>";
							} else {

								return "<div style='background:red;width:20px;height:100%;'></div>";

							}

						}
					},
					{
						type: "numbers",
						width: 50,
						title: "编号",
						templet: function() {
							return "style:'color:red'";
						}
					},
					{
						type: "checkbox",
						//						fixed: "left",
						width: 50
					},

					{
						field: 'typeName',
						title: '隐患类型',
						align: 'center',
						templet: function(p) {
							if(p.typeName == null) {
								return "<div></div>";
							} else if(p.node == 0) {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.typeName + "</div>";
							} else if(p.node == 1) {
								return "<div   onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.typeName + "</div>";

							} else {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.typeName + "</div>";
							}
						}
					},
					{
						field: 'findUser',
						title: '发现人',
						align: 'center',
						templet: function(p) {
							if(p.findUser == null) {
								return "<div></div>";
							} else if(p.node == 0) {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.findUser + "</div>";
							} else if(p.node == 1) {
								return "<div   onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.findUser + "</div>";

							} else {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.findUser + "</div>";
							}
						}
					},
					{
						field: 'findTime',
						title: '发现时间',
						align: 'center',
						templet: function(p) {
							if(p.findTime == null) {
								return "<div></div>";
							} else if(p.node == 0) {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.findTime + "</div>";
							} else if(p.node == 1) {
								return "<div   onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.findTime + "</div>";

							} else {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.findTime + "</div>";
							}
						}
					},
					{
						field: 'auditUser',
						title: '核查人',
						align: 'center',
						templet: function(p) {
							if(p.auditUser == null) {
								return "<div></div>";
							} else if(p.node == 0) {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.auditUser + "</div>";
							} else if(p.node == 1) {
								return "<div   onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.auditUser + "</div>";

							} else {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.auditUser + "</div>";
							}
						}
					},
					{
						field: 'auditStep',
						title: '防范措施完成时间',
						align: 'center',
						templet: function(p) {
							if(p.auditStep == null) {
								return "<div></div>";
							} else if(p.node == 0) {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.finishTime + "</div>";
							} else if(p.node == 1) {
								return "<div   onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.finishTime + "</div>";

							} else {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.finishTime + "</div>";
							}
						}
					},
					{
						field: 'endTime',
						title: '治理期限',
						align: 'center',
						templet: function(p) {
							if(p.endTime == null) {
								return "<div></div>";
							} else if(p.node == 0) {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.endTime + "</div>";
							} else if(p.node == 1) {
								return "<div   onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.endTime + "</div>";

							} else {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.endTime + "</div>";
							}
						}
					},

					{
						field: 'leaderUnitsName',
						title: '责任单位',
						align: 'center',
						templet: function(p) {
							if(p.leaderUnitsName == null) {
								return "<div></div>";
							} else if(p.node == 0) {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.leaderUnitsName + "</div>";
							} else if(p.node == 1) {
								return "<div   onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.leaderUnitsName + "</div>";

							} else {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.leaderUnitsName + "</div>";
							}

						}
					},
					{
						field: 'leaderUser',
						title: '责任人',
						align: 'center',
						templet: function(p) {
							if(p.leaderUser == null) {
								return "<div></div>";
							} else if(p.node == 0) {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.leaderUser + "</div>";
							} else if(p.node == 1) {
								return "<div   onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.leaderUser + "</div>";

							} else {
								return "<div onclick=lookxiangqing('" + p.id + "'," + p.node + ")>" + p.leaderUser + "</div>";
							}
						}
					},
					{
						field: '',
						title: '隐患状态',
						templet: function(p) {
							if(p.node == 0) {
								return "<div id='div" + p.id + "' style=color:#FFB800;font-weight: bold;position: relative; onmouseout=hidedescribe('div" + p.id + "','" + p.describe + "') onmouseover=showdescribe('div" + p.id + "','" + p.describe + "')  onclick=lookxiangqing('" + p.id + "'," + p.node + ")>已防范</div>";
							} else if(p.node == 1) {
								return "<div id='div" + p.id + "' onmouseout=hidedescribe('div" + p.id + "','" + p.describe + "') onmouseover=showdescribe('div" + p.id + "','" + p.describe + "')   style=color:#01AAED;font-weight: bold;  onclick=lookxiangqing('" + p.id + "'," + p.node + ")>已治理</div>";
								//					return "<div style=color:#FFB800;font-weight: bold;  onclick=lookxiangqing('"+p.id+"',"+p.node+")>已治理</div>";
							} else {
								//          		return "<div style=color:#FFB800;font-weight: bold; onclick=lookxiangqing('"+p.id+"',"+p.node+")>已防范</div>";
								return "<div id='div" + p.id + "' onmouseout=hidedescribe('div" + p.id + "','" + p.describe + "') onmouseover=showdescribe('div" + p.id + "','" + p.describe + "')   style=color:#dddddd;font-weight: bold; onclick=lookxiangqing('" + p.id + "'," + p.node + ")>已销项</div>";
							}
						},
						align: "center"
					}
				]
			],
			done: function(res, curr, count) {
				//无论是异步请求数据，还是直接赋值数据，都会触发该回调。可以利用该回调做一些表格以外元素的渲染。
				//如果是异步请求数据方式，res即为你接口返回的信息。
				//如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度

				//
				//                  //得到当前页码
				//
				//                  //得到数据总量
			}
		});

	}

	//是否置顶
	form.on('switch(newsTop)', function(data) {
		var index = layer.msg('修改中，请稍候', {
			icon: 16,
			time: false,
			shade: 0.8
		});
		setTimeout(function() {
			layer.close(index);
			if(data.elem.checked) {
				layer.msg("置顶成功！");
			} else {
				layer.msg("取消置顶成功！");
			}
		}, 500);
	})

	//批量删除
	$(".delAll_btn").click(function() {
		var checkStatus = table.checkStatus('newsListTable'),
			data = checkStatus.data,

			newsId = [];
		var arr2 = new Array();
		for(var i = 0; i < data.length; i++) {
			arr2.push(data[i].id);
		}
		if(data.length > 0) {
			for(var i in data) {
				newsId.push(data[i].newsId);
			}
			layer.confirm('确定删除选中的数据吗？', {
				icon: 3,
				title: '提示信息'
			}, function(index) {
				//                 $.get("删除文章接口",{
				//                     newsId : newsId  //将需要删除的newsId作为参数传入
				//                 },function(data){

				$.ajax({
					url: url + 'perilInfo/delPerilInfo',
					dataType: 'json',
					async: false,
					type: "POST",
					data: {
						id: arr2.toString(),
						Token: salt
					},
					success: function(data) {
						if(data.code == 0) {
							layer.msg('删除成功');
						} else {
							layer.msg(data.msg);
						}

					},
					error: function(error) {
					}
				});

				tableIns.reload();
				layer.close(index);
				//                 })
			})
		} else {
			layer.msg("请选择需要删除的数据");
		}
	})

	//列表操作
	table.on('tool(newsList)', function(obj) {
		var layEvent = obj.event,
			data = obj.data;

		if(layEvent === 'edit') { //编辑
			addNews(data);
		} else if(layEvent === 'del') { //删除
			layer.confirm('确定删除此文章？', {
				icon: 3,
				title: '提示信息'
			}, function(index) {
				// $.get("删除文章接口",{
				//     newsId : data.newsId  //将需要删除的newsId作为参数传入
				// },function(data){
				tableIns.reload();
				layer.close(index);
				// })
			});
		} else if(layEvent === 'look') { //预览
			layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问")
		}
	});

})

function lookxiangqing(riskid, num) {
	if(num == 0) {
		window.location.href = '../risk-particulars/risk-particulars.html?id=' + riskid;
	} else if(num == 1) {
		location.href = '../risk-check/risk-check.html?id=' + riskid;
	} else {
		location.href = '../risk-clear/risk-clear.html?id=' + riskid;
	}

}

function showdescribe(self, cont) {

	layui.use(['layer', 'form'], function() {
		var layer = layui.layer;
		layer.tips(cont, "#" + self, {
			tips: [1, '#ccc'],
			time: 10000,
			area: ['auto', 'auto'] //这个属性可以设置宽高  auto 表示自动
		});
	});
}

function hidedescribe(self, cont) {
	//	$('#' + self).css('display', 'none');
	layer.closeAll();
}